/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DBHandling.AccountHandling;
import DBHandling.RoleHandling;
import Entity.Account;
import Entity.Role;
import Utilities.Encrypt;
import Utilities.MyLibrary;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HuyPV
 */
public class AccountManagementPanel extends javax.swing.JPanel {

    private Vector<Account> accounts;
    private DefaultTableModel model;
    private Account acc;
    private AccountHandling ah;
    private Vector<Role> roles;

    /** Creates new form PanelIndex */
    public AccountManagementPanel() {
        initComponents();
        accounts = new Vector<Account>();
        setOptionTable();
        loadData();

    }

    private void setOptionTable() {
        model = new DefaultTableModel() {

            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        accountTable.setModel(model);

        model.addColumn("User name");
        model.addColumn("Full Name");
        model.addColumn("Phone");
        model.addColumn("Email");
        model.addColumn("Address");
        model.addColumn("Permission");

    }

    //load data from database to table 
    private void loadData() {
        try {
            RoleHandling rh = new RoleHandling();
            roles = new Vector<Role>();
            roles = rh.getRoles();

            MyLibrary.clearAllRow(model);      //clear all row on table
            ah = new AccountHandling();
            accounts = ah.getAccounts();
            for (Account acc : accounts) {
                Vector v = new Vector();
                v.addElement(acc.getUserName());
                v.addElement(acc.getFullname());
                v.addElement(acc.getPhone());
                v.addElement(acc.getEmail());
                v.addElement(acc.getAddress());
                for (Role r : roles) {
                    if (r.getRoleID().equals(acc.getRoleID())) {
                        v.addElement(r.getRoleName());
                        break;
                    }
                }
                model.addRow(v);
            }
            lblTotal.setText("Total account: " + accounts.size());
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
        }

    }

    public void setData(Account acc) {

        txtUserName.setText(acc.getUserName());
        txtFullname.setText(acc.getFullname());
        txtPhone.setText(acc.getPhone());
        txtEmail.setText(acc.getEmail());
        txtAddress.setText(acc.getAddress());
        for (Role r : roles) {
            if (r.getRoleID().equals(acc.getRoleID())) {
                cboPermission.setSelectedItem((r.getRoleName()));
                break;
            }
        }
    }

    public boolean isValidationForm() {
        String user = txtUserName.getText();
        String pass = new String(txtPassword.getPassword());
        String retype = new String(txtRetype.getPassword());
        //String phone = txtPhone.getText();
        //String email = txtEmail.getText();
        //String address = txtAddress.getText();
        if (user.isEmpty()) {
            txtUserName.requestFocus();
            txtUserName.selectAll();
            JOptionPane.showMessageDialog(this, "The user name is not empty!", "Notice", JOptionPane.OK_OPTION);
            return false;
        } else if (user.length() > 20) {
            txtUserName.requestFocus();
            txtUserName.selectAll();
            JOptionPane.showMessageDialog(this, "The user name does not greater than 20 characters!", "Notice", JOptionPane.OK_OPTION);
            return false;
        } else if (pass.length() > 40) {
            txtPassword.requestFocus();
            txtPassword.selectAll();
            JOptionPane.showMessageDialog(this, "The password does not greater than 40 characters!", "Notice", JOptionPane.OK_OPTION);
            return false;
        } else if (!pass.equals(retype)) {
            txtRetype.requestFocus();
            JOptionPane.showMessageDialog(this, "The password does not match!", "Notice", JOptionPane.OK_OPTION);
            return false;
        }
        return true;
    }

    public boolean isExistedUsername(String user) {
        for (Account acc : accounts) {
            if (acc.getUserName().equalsIgnoreCase(user)) {
                return true;
            }
        }
        return false;
    }
    //reset all data in form

    public void resetForm() {
        txtUserName.setText("");
        txtPassword.setText("");
        txtFullname.setText("");
        txtPhone.setText("");
        txtEmail.setText("");
        txtAddress.setText("");
        txtRetype.setText("");
        txtUserName.setEditable(true);
        txtPassword.setEditable(true);
        txtRetype.setEditable(true);
    }

    public Account getCurrentAccount() {
        try {
            String user = txtUserName.getText();
            String fullname = txtFullname.getText();
            String phone = txtPhone.getText();
            String email = txtEmail.getText();
            String address = txtAddress.getText();
            String roleID = roles.get(cboPermission.getSelectedIndex()).getRoleID();
            String pass = Encrypt.encryptMD5(new String(txtPassword.getPassword()));  //ecrypt md5 password
            return new Account(user, pass, fullname, phone, email, address, roleID);
        } catch (NoSuchAlgorithmException ex) {
            JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
        }
        return null;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar7 = new javax.swing.JToolBar();
        jLabel13 = new javax.swing.JLabel();
        btnHome = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        jLabel15 = new javax.swing.JLabel();
        btnEdit = new javax.swing.JButton();
        jLabel16 = new javax.swing.JLabel();
        btnRemove = new javax.swing.JButton();
        jLabel19 = new javax.swing.JLabel();
        btnReset = new javax.swing.JButton();
        jLabel17 = new javax.swing.JLabel();
        btnSearch = new javax.swing.JButton();
        jLabel18 = new javax.swing.JLabel();
        btnHelp = new javax.swing.JButton();
        lblBanner = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtUserName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        cboPermission = new javax.swing.JComboBox();
        txtPassword = new javax.swing.JPasswordField();
        txtRetype = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        txtAddress = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtFullname = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        accountTable = new javax.swing.JTable();
        lblStatus = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setMinimumSize(new java.awt.Dimension(700, 300));
        setPreferredSize(new java.awt.Dimension(747, 591));
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jToolBar7.setBackground(new java.awt.Color(255, 255, 255));
        jToolBar7.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jToolBar7.setFloatable(false);
        jToolBar7.setRollover(true);
        jToolBar7.setEnabled(false);
        jToolBar7.setName("jToolBar7"); // NOI18N

        jLabel13.setText("                         "); // NOI18N
        jLabel13.setToolTipText("");
        jLabel13.setName("jLabel13"); // NOI18N
        jToolBar7.add(jLabel13);

        btnHome.setBackground(new java.awt.Color(255, 255, 255));
        btnHome.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnHome.setForeground(new java.awt.Color(0, 0, 204));
        btnHome.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/home_48.png"))); // NOI18N
        btnHome.setText("    HOME    ");
        btnHome.setFocusable(false);
        btnHome.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnHome.setName("btnHome"); // NOI18N
        btnHome.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnHome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHomeActionPerformed(evt);
            }
        });
        jToolBar7.add(btnHome);

        jLabel14.setText("                    "); // NOI18N
        jLabel14.setToolTipText("");
        jLabel14.setName("jLabel14"); // NOI18N
        jToolBar7.add(jLabel14);

        btnAdd.setBackground(new java.awt.Color(255, 255, 255));
        btnAdd.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnAdd.setForeground(new java.awt.Color(0, 0, 204));
        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/add_item_48.png"))); // NOI18N
        btnAdd.setText(" Add ");
        btnAdd.setFocusable(false);
        btnAdd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAdd.setName("btnAdd"); // NOI18N
        btnAdd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        jToolBar7.add(btnAdd);

        jLabel15.setText("                    ");
        jLabel15.setToolTipText("");
        jLabel15.setName("jLabel15"); // NOI18N
        jToolBar7.add(jLabel15);

        btnEdit.setBackground(new java.awt.Color(255, 255, 255));
        btnEdit.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnEdit.setForeground(new java.awt.Color(0, 0, 204));
        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/edit_item_48.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.setFocusable(false);
        btnEdit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEdit.setName("btnEdit"); // NOI18N
        btnEdit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        jToolBar7.add(btnEdit);

        jLabel16.setText("                    ");
        jLabel16.setToolTipText("");
        jLabel16.setName("jLabel16"); // NOI18N
        jToolBar7.add(jLabel16);

        btnRemove.setBackground(new java.awt.Color(255, 255, 255));
        btnRemove.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnRemove.setForeground(new java.awt.Color(0, 0, 204));
        btnRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/delete_item_48.png"))); // NOI18N
        btnRemove.setText("Remove");
        btnRemove.setFocusable(false);
        btnRemove.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnRemove.setName("btnRemove"); // NOI18N
        btnRemove.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });
        jToolBar7.add(btnRemove);

        jLabel19.setText("                    "); // NOI18N
        jLabel19.setToolTipText("");
        jLabel19.setName("jLabel19"); // NOI18N
        jToolBar7.add(jLabel19);

        btnReset.setBackground(new java.awt.Color(255, 255, 255));
        btnReset.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnReset.setForeground(new java.awt.Color(0, 0, 153));
        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/left_arrow_48.png"))); // NOI18N
        btnReset.setText("Reset form");
        btnReset.setFocusable(false);
        btnReset.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnReset.setName("btnReset"); // NOI18N
        btnReset.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        jToolBar7.add(btnReset);

        jLabel17.setText("                    ");
        jLabel17.setToolTipText("");
        jLabel17.setName("jLabel17"); // NOI18N
        jToolBar7.add(jLabel17);

        btnSearch.setBackground(new java.awt.Color(255, 255, 255));
        btnSearch.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnSearch.setForeground(new java.awt.Color(0, 0, 204));
        btnSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/search_magnifier_48.png"))); // NOI18N
        btnSearch.setText("Search");
        btnSearch.setFocusable(false);
        btnSearch.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSearch.setName("btnSearch"); // NOI18N
        btnSearch.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });
        jToolBar7.add(btnSearch);

        jLabel18.setText("                    ");
        jLabel18.setToolTipText("");
        jLabel18.setName("jLabel18"); // NOI18N
        jToolBar7.add(jLabel18);

        btnHelp.setBackground(new java.awt.Color(255, 255, 255));
        btnHelp.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnHelp.setForeground(new java.awt.Color(0, 0, 204));
        btnHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/help icon 48.png"))); // NOI18N
        btnHelp.setText("   Help   ");
        btnHelp.setFocusable(false);
        btnHelp.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnHelp.setName("btnHelp"); // NOI18N
        btnHelp.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHelpActionPerformed(evt);
            }
        });
        jToolBar7.add(btnHelp);

        add(jToolBar7, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 610, 910, -1));

        lblBanner.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/banner2.jpg"))); // NOI18N
        lblBanner.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        lblBanner.setName("lblBanner"); // NOI18N
        add(lblBanner, new org.netbeans.lib.awtextra.AbsoluteConstraints(-10, 0, 1200, 100));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Account Information", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Sans", 1, 14), new java.awt.Color(0, 0, 255))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText("User name");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Password");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Retype pass");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("Phone");
        jLabel4.setName("jLabel4"); // NOI18N

        txtUserName.setName("txtUserName"); // NOI18N

        jLabel5.setText("Email");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("Permission");
        jLabel6.setName("jLabel6"); // NOI18N

        txtPhone.setName("txtPhone"); // NOI18N

        txtEmail.setName("txtEmail"); // NOI18N

        cboPermission.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrator", "Inventory Manager", "Sales Manager", "Sales Person" }));
        cboPermission.setName("cboPermission"); // NOI18N

        txtPassword.setName("txtPassword"); // NOI18N

        txtRetype.setName("txtRetype"); // NOI18N

        jLabel7.setText("Address");
        jLabel7.setName("jLabel7"); // NOI18N

        txtAddress.setName("txtAddress"); // NOI18N

        jLabel8.setText("Full Name");
        jLabel8.setName("jLabel8"); // NOI18N

        txtFullname.setName("txtFullname"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1)
                        .addComponent(jLabel3))
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cboPermission, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtRetype)
                    .addComponent(txtPassword)
                    .addComponent(txtUserName, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtPhone)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                    .addComponent(txtFullname))
                .addGap(41, 41, 41))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(txtFullname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtRetype, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboPermission, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGap(0, 11, Short.MAX_VALUE))))
        );

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 100, 860, 180));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        accountTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        accountTable.setAlignmentX(0.0F);
        accountTable.setAlignmentY(0.0F);
        accountTable.setColumnSelectionAllowed(true);
        accountTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        accountTable.setName("tableItem"); // NOI18N
        accountTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                accountTableMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(accountTable);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 310, 1000, 290));

        lblStatus.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        lblStatus.setForeground(new java.awt.Color(0, 0, 204));
        lblStatus.setText("Load data complete!");
        lblStatus.setName("lblStatus"); // NOI18N
        add(lblStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 280, -1, 30));

        lblTotal.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        lblTotal.setForeground(new java.awt.Color(0, 0, 255));
        lblTotal.setText("Total account: 0");
        lblTotal.setName("lblTotal"); // NOI18N
        add(lblTotal, new org.netbeans.lib.awtextra.AbsoluteConstraints(670, 280, -1, 30));

        jLabel9.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(255, 0, 0));
        jLabel9.setText("Status: ");
        jLabel9.setName("jLabel9"); // NOI18N
        add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 280, -1, 30));
    }// </editor-fold>//GEN-END:initComponents

        private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed

            if (JOptionPane.showConfirmDialog(this, "Do you wand to remove account : " + acc.getUserName(), "Notice", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
                try {

                    if (ah.removeAccount(acc.getUserName())) {
                        resetForm();
                        loadData();
                        lblStatus.setText("Remove account sucessful!");
                        resetForm();
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
                }
            }
    }//GEN-LAST:event_btnRemoveActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
    }//GEN-LAST:event_btnSearchActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        if (!txtUserName.isEditable()) {
            JOptionPane.showMessageDialog(this, "Please click reset form before adding", "Notice", JOptionPane.OK_OPTION);
            return;
        }
        if (isExistedUsername(txtUserName.getText())) {
            txtUserName.requestFocus();
            txtUserName.selectAll();
            JOptionPane.showMessageDialog(this, "The user name already exist!", "Notice", JOptionPane.OK_OPTION);
        } else if (isValidationForm() && (txtPassword.getPassword() != null)) {
            try {
                if (ah.insertAccount(getCurrentAccount())) {
                    resetForm();
                    loadData();
                    lblStatus.setText("Add new item sucessful");
                    resetForm();
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
            }
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnHomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHomeActionPerformed

        InventoryManagement.callJPanel(new AdministratorPanel());
    }//GEN-LAST:event_btnHomeActionPerformed

    private void accountTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_accountTableMouseReleased
        // TODO add your handling code here:
        try {
            txtUserName.setEditable(false);
            int index = accountTable.getSelectedRow();
            if (index > -1) {
                acc = accounts.get(index);
                setData(acc);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
        }
    }//GEN-LAST:event_accountTableMouseReleased

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        resetForm();
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        String user = txtUserName.getText();
        try {
            if (accountTable.getSelectedRow() > -1) {
                if (!user.equals(acc.getUserName()) && isExistedUsername(user)) {  //
                    txtUserName.requestFocus();
                    txtUserName.selectAll();
                    JOptionPane.showMessageDialog(this, "The user name already exist!", "Notice", JOptionPane.OK_OPTION);
                } else if (isValidationForm() && (txtPassword.getPassword().length == 0)) {
                    try {
                        if (ah.updateAccountWithoutPassword(getCurrentAccount(), acc.getUserName())) {
                            loadData();
                            lblStatus.setText("Update account sucessful!");
                            resetForm();
                        }
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(this, "Error edit: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
                    }
                } else if (isValidationForm() && (txtPassword.getPassword().length != 0)) {
                    try {
                        if (ah.updateAccount(getCurrentAccount(), acc.getUserName())) {
                            loadData();
                            lblStatus.setText("Update account sucessful!");
                            resetForm();
                        }
                    } catch (Exception ex) {
                        JOptionPane.showMessageDialog(this, "Error edit: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
                    }
                }
            } else {
                JOptionPane.showMessageDialog(this, "Please select row that you want to edit!!", "Notice", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Notice", JOptionPane.OK_OPTION);
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void btnHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHelpActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            InventoryManagement.OpenManual();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "cannot locate manual file in 'manual/ISM_Manual.htm'");
        }
    }//GEN-LAST:event_btnHelpActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable accountTable;
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnHelp;
    private javax.swing.JButton btnHome;
    private javax.swing.JButton btnRemove;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cboPermission;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar jToolBar7;
    private javax.swing.JLabel lblBanner;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtFullname;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JPasswordField txtRetype;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
}
